# coding=utf-8
from tkinter import messagebox, Tk, Label, StringVar, Entry, BooleanVar, Checkbutton, END, Button

from service.login_sv import LoginSV
from tools.cachedata import CacheData
from tools.cachedataclient import CacheDataClient
from view.dashboard import DashBoard


class LoginView:
    def __init__(self):
        self.window = Tk()
        self.window.title("华星支付-登录")
        self.window.iconbitmap('favicon.ico')
        # 窗口大小
        width, height = 500, 300
        # 窗口居中显示
        self.window.geometry('%dx%d+%d+%d' % (
            width, height, (self.window.winfo_screenwidth() - width) / 2,
            (self.window.winfo_screenheight() - height) / 2))
        # 窗口最大值
        self.window.maxsize(width, height)
        # 窗口最小值
        self.window.minsize(width, height)
        # 顶部标签
        Label(self.window, text="账 号:").place(x=100, y=120)
        Label(self.window, text="密 码:").place(x=100, y=150)

        # 用户名输入框
        self.var_usr_name = StringVar()
        entry_usr_name = Entry(self.window, width=30, textvariable=self.var_usr_name)
        entry_usr_name.place(x=160, y=120)
        # 密码输入框
        self.var_usr_pwd = StringVar()
        entry_usr_pwd = Entry(self.window, width=30, textvariable=self.var_usr_pwd, show='*')
        entry_usr_pwd.place(x=160, y=150)

        # 记录密码复选框
        self.checkbox = BooleanVar()
        Checkbutton(self.window, text="记住密码", variable=self.checkbox).place(x=160, y=175)

        try:
            cache_data = CacheData().toObj(CacheDataClient().read())
            account = cache_data.get_account
            password = cache_data.get_password
            if account and password:
                entry_usr_name.insert(END, account)
                entry_usr_pwd.insert(END, password)
                self.checkbox.set(True)
        except Exception as e:
            pass

        # body部按钮
        enter_btn = Button(self.window, width=30, text="登 录", command=self.window_init)
        enter_btn.place(x=160, y=250)
        self.window.bind('<Return>', self.short)
        self.window.mainloop()

    def short(self, event):
        self.window_init()

    def window_init(self):
        """
        打开指定页面
        :return:
        """
        user_name = self.var_usr_name.get()
        user_pwd = self.var_usr_pwd.get()
        if not user_name or not user_pwd:
            messagebox.askokcancel('错误提醒', '账户或密码不能为空！')
            return

        login_sv = LoginSV()
        flag = login_sv.login(user_name, user_pwd)
        if flag:
            # 记住密码
            remmeber_pwd = self.checkbox.get()
            if remmeber_pwd:
                cache_data_client = CacheDataClient()
                cache_data = CacheData().toObj(cache_data_client.read())
                cache_data.account = user_name
                cache_data.password = user_pwd
                cache_data_client.write(cache_data.toJson())

            self.window.destroy()

            dashboard = DashBoard()
            dashboard.log("账户 [" + user_name + "] 登录成功！")

        else:
            messagebox.askokcancel('错误提醒', '账户或密码错误！')
            return
